<template>
	<view class="margin-top bg-white padding-lr">
		<u-form :model="info" ref="form" label-width="auto">
			<u-form-item label="执行器" prop="execId" required>{{info.execName}}</u-form-item>
			<u-form-item label="任务名称" prop="name" required><u-input v-model="info.name" /></u-form-item>
			<u-form-item label="任务编码" prop="code" required><u-input v-model="info.code" /></u-form-item>
			<u-form-item label="任务端点" prop="endpoint" required><u-input v-model="info.endpoint" /></u-form-item>
			<u-form-item label="Cron表达式" prop="cron" required><u-input v-model="info.cron" /></u-form-item>
			<u-form-item label="重试次数" prop="retry" required><u-input v-model="info.retry" type="number" /></u-form-item>
			<u-form-item label="负责人" prop="person"><u-input v-model="info.person" /></u-form-item>
			<u-form-item label="通知邮箱" prop="email"><u-input v-model="info.email" /></u-form-item>
			<u-form-item label="任务参数" prop="params"><u-input v-model="info.params" /></u-form-item>
			<view class="padding-tb"><u-button type="primary" @click="save">保存</u-button></view>
		</u-form>
	</view>
</template>

<script>
export default {
	onLoad: function(option) {
		if (option.info) {
			this.info = JSON.parse(decodeURIComponent(option.info));
		} else {
			this.info.execId = option.execId || '';
			this.info.execName = option.execName || '';
		}
	},
	onReady: function() {
		this.$refs.form.setRules(this.rules);
	},
	data() {
		return {
			show: false, // 显示选项
			info: {
				id: '',
				execId: '',
				execName: '',
				code: '',
				name: '',
				status: '',
				endpoint: '',
				cron: '0 0/5 * * * ?',
				retry: '1',
				email: '',
				params: ''
			},
			rules: {
				execId: [{ required: true, message: '必填', trigger: ['change', 'blur'] }],
				code: [{ required: true, message: '必填', trigger: ['change', 'blur'] }],
				name: [{ required: true, message: '必填', trigger: ['change', 'blur'] }],
				status: [{ required: true, message: '必填', trigger: ['change', 'blur'] }],
				endpoint: [{ required: true, message: '必填', trigger: ['change', 'blur'] }],
				cron: [{ required: true, message: '必填', trigger: ['change', 'blur'] }]
			}
		};
	},
	methods: {
		// 保存用户
		save: function() {
			this.$refs['form'].validate(valid => {
				if (valid) {
					this.$request.post(
						'/admin/sys/sched/job/save',
						this.info,
						res => {
							this.$utils.showModal('保存成功！', () => {
								uni.navigateBack({
									delta: 1
								});
							});
						},
						false,
						true,
						true
					);
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped></style>
